package com.magic.smartcity.view.adapters;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.magic.smartcity.beans.PressBean;
import com.magic.smartcity.R;
import com.magic.smartcity.utils.Constant;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @create: Create by Magic on 2022/2/2.
 */
public class HomeNewsAdapter extends RecyclerView.Adapter<HomeNewsAdapter.ItemHolder> {
    private static final String TAG = HomeNewsAdapter.class.getSimpleName();
    private List<PressBean.RowsBean> mPresses = new ArrayList<>();

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_topic, parent, false);

        return new HomeNewsAdapter.ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
        PressBean.RowsBean press = mPresses.get(position);
        holder.itemView.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("press",mPresses.get(position));
            bundle.putInt("id",mPresses.get(position).getId());
            Navigation.findNavController(v).navigate(R.id.newsDetailFragment,bundle);
        });
        ImageView image = holder.itemView.findViewById(R.id.image);
        TextView title = holder.itemView.findViewById(R.id.title);
        TextView content = holder.itemView.findViewById(R.id.content);
        TextView comment = holder.itemView.findViewById(R.id.comment);
        TextView time = holder.itemView.findViewById(R.id.time);


        Picasso.get().load(Constant.Service_IP + press.getCover()).into(image);
        title.setText(press.getTitle());
        content.setText(press.getContent());
        comment.setText(press.getCommentNum() + "条评论");
        time.setText(press.getCreateTime() + "发布");
    }

    public void setData(List<PressBean.RowsBean> presses) {
        if (presses != null) {
            mPresses.clear();
            mPresses.addAll(presses);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mPresses != null ? mPresses.size() : 0;
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
